package chances.epg.data.taglib;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import chances.epg.commons.utils.SpringWebUtils;
import chances.epg.config.EpgSiteContext;
import chances.epg.navigator.EpgConstants;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.service.AccessContextFactory;

@SuppressWarnings("serial")
public class AccessContextTag extends TagSupport{
	private String siteCode;
	private String bizCode;
	private String categoryCode;
	
	
	@Override
	public int doEndTag() throws JspException {
		AccessContextFactory accessContextFactory = SpringWebUtils.findBean(pageContext, AccessContextFactory.class);
		 AccessContext accessContext = accessContextFactory.createAccessContext((HttpServletRequest) pageContext.getRequest());
		 accessContext.setBizCode(bizCode);
		 accessContext.setSiteCode(siteCode);
		 EpgSiteContext siteContext = EpgSiteContext.get(pageContext.getServletContext(), siteCode);
		 accessContext.setEpgSiteContext(siteContext);
		 if(StringUtils.isNotEmpty(this.categoryCode)){
			 accessContext.setContainerType("category");
			 accessContext.setContainerCode(this.categoryCode);
		 }
		 pageContext.getRequest().setAttribute(EpgConstants.EPG_CONTEXT, accessContext);
		 reset();
		 
		return super.doEndTag();
	}
	
	
	@Override
	public void release() {
		super.release();
		this.reset();
	}


	private void reset() {
		
		 this.siteCode = null;
		 this.categoryCode = null;
		 this.bizCode = null;
	}
	public String getSiteCode() {
		return siteCode;
	}
	public String getBizCode() {
		return bizCode;
	}
	public String getCategoryCode() {
		return categoryCode;
	}
	public void setSiteCode(String siteCode) {
		this.siteCode = siteCode;
	}
	public void setBizCode(String bizCode) {
		this.bizCode = bizCode;
	}
	public void setCategoryCode(String categoryCode) {
		this.categoryCode = categoryCode;
	}
	
}
